package cn.audit.controller.role;

import cn.audit.aop.myannotation.BussAnnotation;
import cn.audit.bean.dto.BatchProcessing;
import cn.audit.bean.dto.UserCorrelation;
import cn.audit.bean.pojo.AuditInstitution;
import cn.audit.bean.pojo.RoleManager;
import cn.audit.service.institution.InstitutionService;
import cn.audit.service.role.RoleService;
import cn.audit.service.user.UserService;
import cn.audit.utils.PagerUtil;
import cn.audit.utils.Paging;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 李忆枫
 * @Date: ${date} ${time}
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;

    @Resource
    private InstitutionService institutionService;

    @RequestMapping("/RoleManager")
    public String RoleManager(Model model,
                              @RequestParam(value = "page", required = false) Integer page,
                              @RequestParam(value = "department", required = false) Integer department) {

        List<RoleManager> roleManagers = roleService.findAll();
        model.addAttribute("roleManagers", roleManagers);

        int page1 = 1;
        if (page != null) {
            page1 = page;
        }

        //用户
        List<UserCorrelation> userAll = userService.findUserAll(department);
        PagerUtil pu = Paging.findBook(page1, userAll);
        model.addAttribute("userAll", pu);

        //部门
        List<AuditInstitution> institutions = institutionService.findAll();
        model.addAttribute("institutions", institutions);
        //角色
        List<RoleManager> roleList = roleService.findAll();
        model.addAttribute("roleList", roleList);
        //当前页数
        model.addAttribute("pager", page1);

        return "role/RoleManager";
    }

    /**
     * 保存修改
     *
     * @return
     */
    @RequestMapping("/RoleSave")
    @BussAnnotation(moduleName = "角色管理", type = "修改", option = "修改角色成功")
    public String RoleSave(RoleManager role) {
        roleService.save(role);
        return "redirect:RoleManager";
    }

    /**
     * 删除角色
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @GetMapping("/AjaxDelete")
    @BussAnnotation(moduleName = "角色管理", type = "删除", option = "删除角色成功")
    public Boolean AjaxDelete(BatchProcessing ids) {
        List<Long> id = ids.getIds();
        roleService.deleteById(id);
        return true;
    }

    @ResponseBody
    @GetMapping("/AjaxRoleById")
    public Map<String, Object> AjaxRoleById(Long id) {
        //创建一个map，用来封装数据
        Map adminMap = new HashMap(16);
        System.out.println("------------" + id);
        RoleManager roleById = roleService.findById(id);
        adminMap.put("roleById", roleById);

        return adminMap;
    }


}
